home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2156 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  18.0 KB

  1. Path: ix.netcom.com!netnews
  2. From: mcorcora@ix.netcom.com(Marian Corcoran )
  3. Newsgroups: comp.lang.c++
  4. Subject: STL FAQ, January  15, 1996
  5. Date: 16 Jan 1996 00:57:42 GMT
  6. Organization: Netcom
  7. Message-ID: <4det66$rpg@ixnews3.ix.netcom.com>
  8. References: <4decne$2ar@moon.glo.be> <ALUN.CHAMPION.96Jan15171419@g7240065.bridge.bst.bls.com>
  9. NNTP-Posting-Host: ix-sj26-01.ix.netcom.com
  10. X-NETCOM-Date: Mon Jan 15  4:57:42 PM PST 1996
  11.  
  12.  
  13. C++ STL FAQ
  14.    This FAQ deals with the containers, iterators,
  15. and algorithms sections of the C++ Standard, 
  16. Clauses 23, 24 and 25.
  17.  
  18. If you have any questions you had while working with the
  19. STL and answers you received or worked out, please
  20. submit them to me for inclusion. I will give you credit
  21. for it if you include your name. 
  22.  
  23. Thanks,
  24. Marian Corcoran
  25. mcorcora@ix.netcom.com
  26. Copyright (c) 1995, Marian Corcoran
  27. This may be freely distributed, but not sold. 
  28. No guarantees.
  29.  
  30. update 1/15/96
  31.  
  32. Glass, Graham.  "Extending the STL."  C++ Report.  January, 1996
  33. Whitepaper to use STL with VC++ 4
  34.     www.microsoft.com/devonly/visualc
  35.  
  36. update 12/1/95
  37.  
  38. 1. New Books and Articles:
  39.    Glass, Graham.  STL Primer.  Prentice-Hall, 1995.
  40.      Based on ObjectSpace Manual.
  41.    Musser, David.  STL Tutor and Reference.  Addison-Wesley, 1995.
  42.    Nelson, Mark.  C++ Programmer Guide to STandard Template
  43.       Library Programming. IDG Books, 1995.
  44.      Good introduction.  
  45.    Plauger, P.J.  Has an article on STL in C/C++ Users
  46.       Journal, Dec 1995.
  47. 2. Added questions 6 and onward in Q & A part.
  48.  
  49. 3. RogueWave STL to be included in Borland C++ Compiler.
  50.  
  51. 4. The following comes from one of my students in my STL course:
  52.    After managing to install the ObjectSpace STL<Toolkit> with the    
  53.    Watcom   
  54.    C++/C 10.5 compiler, I wrote notes on the subject in FAQ format.  
  55.    The experience was positive - the two work well together. 
  56.    I humbly hope that this may be of others who need to install this   
  57.    version of the STL under a compiler which is not Borland, MS, or
  58. gnu,   
  59.    or on a platform other than DOS/Win or unix (I used Warp). 
  60.    I placed the ASCII text version of the faq in: 
  61.    ftp.netcom.com/pub/me/medina/C++/objectspace_and_watcom_faq.txt 
  62.    (with a web browser, place the string, "ftp://" ahead of that
  63. address.) 
  64.    -- Sean Medina
  65. update 9/2/75
  66.    Two excellent articles have appeared on the STL
  67.  
  68. Dawes, Beman.  "You Can Count On It."  C/C++ Users 
  69.     Journal, July, 1995.  Excellent intro material.
  70. Zigmond, Dan. "Generic Programming and the C++ STL."
  71.     Dr. Dobb's Journal, August, 1995.  Another good
  72.     sample program.
  73.  
  74. Rogue-Wave will be providing the STL to the following
  75. vendors: HP, SGI, SunSoft, Siemens-Nixdorf
  76.  
  77. update 7/3/95
  78.    Started adding new sections on questions 
  79.  
  80. Of special note:  A major complaint about libraries that
  81. programmers have is that you don't get the source.  Well, 
  82. with STL, you get the source.  STL source is 
  83. also excellent code to study.  It is very well written code. 
  84.  
  85. CONTENTS
  86.  
  87. I have put together four articles for the STL FAQ:
  88.   I.    Sources 
  89.   II.   Questions
  90.   III.  References of STL (M. Corcoran).  
  91.   IV.   Report from Lockheed on their first experiences 
  92.         with STL.  
  93.   V.    On using MODENA by Edgar Crisostomo (along with some
  94.         needs in education that he sees for STL)
  95.   VI.   Comparison of Modena and ObjectSpace by Mike Lindner. 
  96.  
  97. REQUEST FOR OTHER MATERIAL  If someone has other material 
  98. or experiences to include you may e-mail them to me and 
  99. I will add them.  Especially of interest would be 
  100. examples of STL that you may have developed as you 
  101. learn STL or industrial applications or porting 
  102. instructions for different platforms.  Please state 
  103. whether I may use your article in the FAQ and whether 
  104. I may use your name.
  105.  
  106. Marian
  107. ===============================================================
  108. PART I  SOURCES
  109.  
  110. 1.  Where does one get the STL?
  111.  
  112.     A. anonymous ftp via butler.hpl.hp.com 
  113.         works with Borland 4.5 and IBM
  114.         also includes something on hash tables 
  115.         not in STL by Bob Fraley bfhash.zip
  116.             and David Musser dmhash.zip
  117.            no support included
  118.         to use the public domain version 
  119.             of STL with BC4.5
  120.         Projects|Defines  and type __MINMAX_DEFINED
  121.         or use #define __MINMAX_DEFINED before
  122.         #include <iostream.h>
  123.  
  124.     B. STL++: Modena Software at 1-800-MODENA-1
  125.         works with Borland, IBM C Set++, Apogee, 
  126.             Visual C++ 2.0 (this last has some limitations.)
  127.         comes with tutorial, you can get the tutorial
  128.                
  129.             separately (read this tutorial after A. Stevens 
  130.             and after playing with ObjectSpace examples.)
  131.  
  132.     C. STL<ToolKit>:ObjectSpace at 1-800-OBJECT-1
  133.         most C++ compilers
  134.         code is well documented
  135.           comes with tutorial with over 200 elementary
  136.             examples that have just been placed in the 
  137.       public domain available
  138.       via anonymous ftp via butler.hpl.hp.com at
  139.                 /STL/examples.Z or .zip for PC
  140.         These examples are a good place to start. 
  141.  
  142.  
  143.      D. libstdc++ (part of libg++):    Free Software Foundation
  144.         anonymous ftp via prep.ai.mit.edu and many other
  145.         machines only some containers exist, but works with GNU
  146. C++, 
  147.       and probably many other C++ compilers
  148.  
  149.      E.  RogueWave to include STL, coming out in first quarter, 1996
  150.  
  151. ===============================================================
  152. PART II  QUESTIONS
  153.  
  154. 1.  What are the iterators, containers, and algorithms 
  155. sections of the  STL (Standard Library)?
  156. A.  The STL has a is a data structures or container class 
  157. library that has been adopted into the language.  It 
  158. consists of three major components:
  159.     Containers or data structures
  160.         Iterators
  161.     Algorithms
  162.  
  163. 2.  What are containers?
  164. Containers include such things as vectors, lists, queues, 
  165. priority queues, stacks, maps, and sets.  In STL, 
  166. containers (data structures) are templatized.  For 
  167. example, the stack class may be used with integers, 
  168. doubles, and user defined types.
  169.  
  170. 3.  What are iterators?
  171. Iterators may be thought of as the key to STL, acting 
  172. as an intermediary between the algorithms and the 
  173. containers.  You are already familiar with the concept 
  174. of an iterator when you think of the pointer used to 
  175. traverse an array.  Iterators are objects in STL.  
  176. One may think of them as a finger moving across the 
  177. elements of a container.  The five categories of 
  178. iterators are:
  179.  
  180.    Random Access -> Bidirectional-> Forward -> Input
  181.                             -> Output
  182. 4.  How are iterators related to containers and
  183. algorithms?
  184. One may think of each container and each algorithm as 
  185. being associated with a certain iterator.  A vector has 
  186. a random access iterator, therefore it may use a random 
  187. access algorithms such as a sort.  A container may use 
  188. any algorithm associated with its iterator or any 
  189. algorithm associated with an iterator to the right of 
  190. it in the above diagram. Therefore a vector (random 
  191. access iterator) may use algorithms associated with 
  192. bidirectional, forward, input or output iterators.  
  193. Can a vector be used with the binary_search algorithm 
  194. (takes a forward iterator) ?  If we look at the diagram 
  195. above, forward is to the right of random access, 
  196. therefore we may use binary_search with a vector 
  197. container.  Can a vector be used with copy (takes an 
  198. input and an output iterator)?  Since input and output 
  199. are to the right of random access, vector may used 
  200. with copy.
  201.  
  202. There are quite a number of algorithms in STL, 
  203. including count(), copy(), replace( ), reverse( ), ...
  204. Just as each container is associated with a certain 
  205. iterator, each algorithm is also associated with a 
  206. certain iterator(s).
  207.  
  208. count( ) - input iterator
  209. copy ( ) - input and output iterator
  210. replace( ) - forward iterator
  211. reverse( ) - bidirectional iterator
  212.  
  213. 6.  I got the ObjectSpace examples and they don't work 
  214. with the HP STL.  I get an error message that there 
  215. is no Stl.h file.
  216.  
  217. A.  ObjectSpace compiled all the different files of the 
  218. STL together.  This is helpful when first learning 
  219. it.  However, later you will probably want to include 
  220. just the files you need.  
  221.      A rule of thumb here is to always include the algo.h 
  222. or algorith.h (latter for ObjectSpace) file and then 
  223. nclude the file with the container you are using:
  224.  
  225. list - list.h
  226. stack, queue, priority_queue - stack.h
  227. deque - deque.h
  228. vector - vector.h
  229. map - map.h
  230. multimap - multimap.h
  231. set - set.h
  232. multiset - multiset.h
  233.  
  234. 7.  When I try to use HP STL with Borland 4.5, 
  235. I get an error function min and max already 
  236. defined.  Why?
  237.  
  238. The functions min( ) and max( ) are already defined 
  239. in the stdlib.h file.  However, there is #ifdef to 
  240. compile them only if they are already not defined.  
  241. Therefore, you may place a __MINMAX_DEFINED  line in 
  242. your Project|Defines window of put the line 
  243. #define __MINMAX_DEFINE before the 
  244. #include <iostream.h>
  245.  
  246. 8.  Where is the code for the STL?  When I downloaded
  247. STL from the HP site, I only got header files.
  248.  
  249. The code for the STL is in the .h files
  250.  
  251. 9.  I hear the STL is extensible.  What does that mean?
  252. How will different vendors extend the STL?
  253.  
  254. Extensible means that one will be able to add algorithms,
  255. containers, and iterators to the STL.
  256. Joe Buck (jbuck@synopsis.com) writes:
  257. Rogue Wave is now giving a January 1996 date for STL
  258. inclusion.  See
  259.  
  260. http://www.roguewave.com/rwpav/products/stdlibwp.htm
  261.  
  262. for discussion of their plans for including STL support
  263. in their Tools.h++ class library.
  264.  
  265. Reading that paper, and looking at what ObjectSpace has
  266. done (see http://www.objectspace.com/products/stl/), it
  267. seems that vendors are making their own enhancements to
  268. the libraries, generally in the same direction: to make
  269. the library somewhat less low-level, to provide cleaner
  270. interfaces for the very common
  271.  
  272.     algorithm(container.begin(), container.end())
  273.  
  274. so that one can write something like algorithm(container)
  275. (in ObjectSpace's case) or container.algorithm() (in
  276. Rogue Wave's case).
  277.  
  278. Perhaps the committee should consider doing a little bit in
  279. this area, before every library vendor does it differently?
  280. It seems that there are fairly natural interfaces that could
  281. be provided to cover the common cases where a range that
  282. is the entire container is to be specified, where the user
  283. is thinking "sort the container" or "copy container A to
  284. container B".  If the committee doesn't provide them, they
  285. will still be there, based on what vendors are already
  286. doing, but they will be non-portable.
  287.  
  288.  
  289.  
  290.  
  291. ==============================================================
  292. PART III  REFERENCES
  293.  
  294. A. PERIODICALS
  295.     D. Jordan. ODMG Update: Collections in ODMG-93
  296.        Discusses ODMG and STD LIB.  C++ Report, June 1995
  297.     A. Koenig. File iterators. Journal of Object-oriented
  298.      Programming  (JOOPS), Nov/Dec 1994
  299.     A. Koenig. Generic iterators. JOOPS, Sept. 1994
  300.     A. Koenig. Templates and generic iterators.  
  301.     JOOPS, June 1994
  302.     A. Koenig. ? . January , 1995
  303.     D.R. Musser and A.A. Stepanov. Algorithm-oriented 
  304.        generic libraries.  Software-Practice and Experience, 
  305.        July 1994
  306.     N. Meyers. A New and Useful Template Technique: 
  307.        "Traits" C++ Report, June 1995
  308.     A.A. Stepanov and M.Lee.  The Standard Template 
  309.       Library. ISO Programming Language C++ Project. 
  310.       Doc. No. X3J16/94-0095, WG21/N0482, May 1994.  
  311.       (Look in HP's ftp for stl.doc, I believe). 
  312.        Although it is the 
  313.     "definitive" STL, it is not intro material. 
  314.     A. Stevens. He has an interview with Stepanov in
  315.       Dr. Dobb's Journal, March 1995.  This is an 
  316.       excellent conceptual introduction to the STL, 
  317.       also a good place to start.
  318.     A. Stevens.  The Standard Template Library 
  319.        (with some code) in his column, Dr. Dobb's Journal, 
  320.        April 1995.  Also good intro material.
  321.     B. Stroustrup. Making a vector fit for a standard. 
  322.         C++ Report, Oct. 1994. intermediate level 
  323.         material. Stroustrup made some important 
  324.         contributions to the design of STL, discussed 
  325.         here.
  326.     M.J. Vilot. An introduction to the standard template
  327.      library.  C++ Report, Oct 1994. You might want to read
  328.     Stevens and Dawes first.
  329.     Also see 
  330.     http://www.cs.rpi.edu/~musser/STL.html
  331.     This material is also available via anonymous ftp
  332.        ftp.cs.rpi.edu in directory pub/STL the file 
  333.     STL-info.ps.Z
  334.      includes STD LIB Online Algorithm Reference 
  335.       by R. Cook, 
  336.     D. Musser, and K Zalewski with examples.
  337.     See ObjectSpace examples first.
  338.  
  339.   B.  STL Web Pages - "Newbie" guide
  340.     http://weber.u.washington.edu/~bytewave/STL.html
  341.    1.  Intro material including Stepanov Speaks on 
  342.        STD LIB in C++     (M. Corcoran),
  343.    2.  Dr. Dobbs article (A. Stevens), STD LIB newbie 
  344.        notes (M. Khan),
  345.    3. STD LIB by Stepanov & Lee, STD LIB files, and 
  346.       ANSI C++ Draft (HTML version)
  347.  
  348.  
  349. ==============================================================
  350. PART IV. EXPERIENCES FROM LOCKHEED
  351.  
  352. We've just obtained STL<ToolKit> for use with our HP 
  353. cfront compiler and the Softbench development 
  354. environment. Here are the first impressions & observations:
  355.  
  356. Apparently ObjectSpace doesn't routinely deliver 4mm DAT 
  357. tapes and added $25 to do it. The tape we recieved was 
  358. DOA (unreadable) but the ObjectSpace support folks quickly 
  359. e-mailed the TAR-file and we were in bussiness. We did 
  360. not experience the problems reported by Mike Linder 
  361. contacting the ObjectSpace people. The above problem w
  362. as solved within two hours.
  363.  
  364. Our experience with installing and building the 
  365. libraries supports Mike Linder's observations. 
  366. Good job here by the vendor.
  367.  
  368. Compiling with the debug option (-g) resulted in many 
  369. warnings similar **to the following:
  370.  
  371. CC: "release1.C", line 28: warning: 
  372. debug.emit_type_entry:typedef node has no 
  373. symid_ptr: vector <X*> ::size_type (187)
  374. CC: "release1.C", line 28: warning: 
  375. debug.emit_type_entry:typedef node 
  376. **has no symid_ptr: vector <X*> ::const_reference (187)
  377.  
  378. The executable does run and the debugger still works, 
  379. however.
  380.  
  381. Note that the person evaluating STL<ToolKit> is 
  382. not an experienced C++ programmer and is proceding 
  383. on the strength of a C++ class. His first impressions 
  384. of the ObjectSpace documentation are favorable and 
  385. he was able to write a simple program from a 
  386. standing-start in a few hours. We also have the 
  387. documentation from HPs public-domain STD LIB. 
  388. I'll keep you posted.
  389.  
  390. ===============================================================
  391. PART V. ON USING MODENA at SIEMENS-ROLM
  392.  
  393.    We use Modena STL++ v2.0 - they respond fairly 
  394. quickly to our needs, they add bug fixes or compiler 
  395. support for our Unixware 2.0 SDK C++ compiler, and 
  396. they answer our many technical questions.
  397.  
  398. The STD LIB++ Manual has a "Files" Heading at the top 
  399. of each component description, and that is all that 
  400. should be explicitly #included to use that component 
  401. (mutual independence).
  402.  
  403. When I #include <vector.h>, it pulls in all the other 
  404. dependent includes.  It is not explicitly stated, but 
  405. for C-like arrays, you #include <algobase.h>.
  406.  
  407. There are minor problems, I will report to Modena 
  408. (example: hashfun.h has non-template function 
  409. _definitions_, so you get multiply defined, when 
  410. used over several translation units.   To improve 
  411. the ability (for us) to specify manual template 
  412. instantiation, it would be nice if they factor out 
  413. the non-inline template definitions into another file.
  414.  
  415. EDUCATIONAL HINTS
  416. A discussion of build issues would be useful.  Especially 
  417. since STL template code can have really obscure 
  418. compiler messages with todays compiler technology.  
  419. There are also issues with debugging template code.
  420.  
  421. A discussion of when to pass container by reference, 
  422. versus passing iterators would be useful.
  423.  
  424. Edgar Crisostomo    408-492-6528, 
  425. edgar@clipper.robadome.com
  426. Systems Software, 
  427. Siemens Rolm Communications, Inc. FAX 408-492-3305  
  428.  
  429. =============================================================
  430. PART VI
  431. COMPARISON OF MODENA AND OBJECTSPACE 
  432.  
  433. I just bought STL++ from Modena and 
  434. STL<toolkit> from ObjectSpace, for purposes of 
  435. comparison.  So far I have not had a chance to use 
  436. either extensively, but I have the following observations:
  437.  
  438. Modena was very helpful on the phone, and responded 
  439. quickly to whatever I called about.  The initial 
  440. floppy they sent me was bad, and they replaced it 
  441. overnight.  In every case but one technical question, 
  442. the person who answered the phone was able to do 
  443. everything I needed.  In the remaining case, I 
  444. received a call back within hours of my question.  
  445. I like their service.
  446.  
  447.  ObjectSpace had an operator, who directed me to the 
  448. one salesperson, who was always "out of the office" 
  449. when I called, and who was the only one who could 
  450. sell me the product (it took 5 days just to order 
  451. the product, because I couldn't get in touch with her).
  452.  
  453. Both products come as source, and you must build the 
  454. libraries yourself.  The documentation for doing this 
  455. from Modena was sketchy, and it wasn't until I called 
  456. tech support that I discovered I had to change some 
  457. files by hand to make them compile with my compiler 
  458. (which is one of the one's they advertise as working with).
  459.  
  460. ObjectSpace, on the other hand, has a neat little 
  461. config package that tests what your compiler can do, 
  462. and writes a header file which configures the code 
  463. to be correct for your compiler (lots of preprocessor 
  464. magic in them there header files).  It was a joy to use.
  465.  
  466. So far, the test programs I have compiled are much 
  467. smaller when I use the ObjectSpace libraries than 
  468. with the Modena libraries.  As I said, I have not 
  469. stressed either product, so these numbers may not 
  470. reflect use in an actual application.
  471.  
  472. As for conformance, ObjecSpace doesn't necessarily support 
  473. everything in STL, but only as much as they can 
  474. squeeze out of the compiler you give them to work 
  475. with.  Is that good or bad?
  476.  
  477. The ObjectSpace library claims to be "thread safe", 
  478. although I haven't tried that our yet.  Apparently 
  479. they have a wrapper class that behaves like a smart 
  480. pointer, but can be locked for reading or writing.  
  481. They also have some other platform independent thread 
  482. and mutex code.
  483.  
  484.   The Modena folks said their library is not 
  485. "thread safe", but only in one place, and they would 
  486. gladly show me how to modify the source to add that 
  487. feature.  One of my compiler vendors says they'll be 
  488. shipping Modena with their compiler soon, and their 
  489. version will be thread safe.
  490.  
  491. Modena includes some extra goodies, like hash tables 
  492. and an ANSI string class.
  493. ObjectSpace has added some features recently as well.
  494.  
  495. Mike Lindner
  496. mikel@attmail.com
  497. mpl@cmprime.attpls.com
  498. mpl@pegasus.att.com
  499.  
  500.  
  501.